package homework02;

import java.util.Arrays;
import java.util.Scanner;

public class Three {
	public static void main(String args[])
	{
		Scanner sc = new Scanner(System.in);
		String n=sc.nextLine();
		String[] id=new String[Integer.parseInt(n)];
		for(int i=0;i<Integer.parseInt(n);i++)
		{
			id[i]=sc.nextLine();
		}
		
		while(true)
		{
			
			
			String choice = sc.nextLine();
			switch(choice)
			{
				case "sort1":
					String[] data=new String[Integer.parseInt(n)];
					for(int i=0;i<Integer.parseInt(n);i++)
						data[i]=id[i].substring(6, 10)+"-"+id[i].substring(10, 12)+"-"+id[i].substring(12, 14);
					Arrays.sort(data);
					for(int i=0;i<Integer.parseInt(n);i++)
					{
						System.out.println(data[i]);
					}
					break;
				case "sort2":
					String[] data2 = new String[Integer.parseInt(n)];
					for(int i=0;i<Integer.parseInt(n);i++){
						data2[i] = id[i].substring(6, 14);
					}
					for(int i=0;i<Integer.parseInt(n);i++){
						for(int j=0;j<Integer.parseInt(n)-1;j++){
							String pre = data2[j];
							int pre1=Integer.parseInt(pre);
							String next = data2[j+1];
							int next1=Integer.parseInt(next);
							if(pre1>next1){
								String t = data2[j];
								data2[j] = data2[j+1];
								data2[j+1] = t;
								String t2 = id[j];
								id[j] = id[j+1];
								id[j+1] = t2;
								
							}
						}
					}
					for(int i=0;i<Integer.parseInt(n);i++){
						System.out.println(id[i]);
					}
					
					break;
				default:
					System.out.println("exit");
					break;
			}
			
		}
	}
}
